// import axios from 'axios';

export const debug = process.env.NODE_ENV !== 'production' && 0;

let base = '';
if (!debug) {
	base = window.api_base;
}

// base = window.api_base;
function axios(method, url, param) {
	return new Promise(function (resolve, reject) {
	  $.ajax({
		method: method,
		url: url,
		data: param,
		xhrFields: {
		  withCredentials: true
		},
		success: function (data) {
			let res = {
				data:data
			}
		  resolve(res);
		},
		error: function (error) {
		  reject(error)
		}
	  });
	})
  };
const defaultParams = {};

function mixParams(params) {
	for (const k in defaultParams) {
		if (!params.hasOwnProperty(k)) {
			params[k] = defaultParams[k];
		}
	}
	return params;
}

export function setActivityId(id) {
	defaultParams.activity_id = id;
}

export function getUserList(params) {
	return axios('post',`${base}/api-user.html`, mixParams(params)).then(res => {
		console.log(res,'获取人员列表')
		if (!res.data.code) {
			// 如果后台返回格式和前端不一致，可以在这里进行格式转换，从而避免去每个页面进行修改
			formatUserList(res.data.data.list);
		}
		return res.data;
		// if (!res.code) {
		// 	// 如果后台返回格式和前端不一致，可以在这里进行格式转换，从而避免去每个页面进行修改
		// 	formatUserList(res.data.list);
		// }
		// return res.data;
	});
}

export function getMusic(params) {
	return axios('post',`${base}/api-activity.html`, mixParams(params)).then(res => {
		return res.data;
	});
}


export function getPrize(params){
	return axios('post',`${base}/api-prize.html`, mixParams(params)).then(res => {
		if (!res.data.code) {
			// format to local
			for (const o of res.data.data.list) {
				if (o.id) {
					o.value = o.id;
				}
			}
		}
		return res.data;
	});
}

/**
 * @params { prize_id, num, }
 **/
export function getLottery(params){
	return axios('post',`${base}/api-lottery.html`, mixParams(params)).then(res => {
		if (!res.data.code) {
			formatUserList(res.data.data.list);
		}
		return res.data;
	});
}

export function getLotteryResult(params){
	return axios('post',`${base}/api-winner.html`, mixParams(params)).then(res => {
		if (!res.data.code) {
			// format to local
			for (const g of res.data.data.list) {
				formatUserList(g.list);
				for (const o of g.list) {
					if (!o.hasOwnProperty('state')) {
						// 是否已领奖
						o.state = o.is_accept;
					}
				}
			}
		}
		return res.data;
	});
}
/**
 *  删除单个人
 * @params {
 * 		activity_id 活动ID
		user_id 用户ID
	}
 * */
export function deleteOneMan(params){
	return axios('post',`${base}/api-cancel.html`, mixParams(params)).then(res => {
		return res.data;
	});
}

/**
 * 取消本次抽奖结果
 * @params { prize_id }
 */
export function cancelLottery(params){
	return axios('post',`${base}/api-clear.html`, mixParams(params)).then(res => {
		return res.data;
	});
}

/**
 * @params { user_id }
 **/
export function acceptPrize(params){
	return axios('post',`${base}/api-accept.html`, mixParams(params)).then(res => {
		return res.data;
	});
}

/**
 * @params { id }
 **/
export function deleteWinner(params){
	return axios('post',`${base}/api-delete.html`, mixParams(params)).then(res => {
		return res.data;
	});
}

/**
 * @params { prize_id[opt] }
 **/
export function emptyWinner(params){
	return axios('post',`${base}/api-truncate.html`, mixParams(params)).then(res => {
		return res.data;
	});
}


export function sendSms(params) {
	return axios('post',`${base}/api-sms.html`, mixParams(params)).then(res=>{
		return res.data;
	});
}
// 格式化为本地格式
export function formatUserList(list) {
	for (const o of list) {
		if (!o.hasOwnProperty('head')) {
			o.head = o.image_head_url;
		}
		if (o.detail) {
			const detail = JSON.parse(o.detail);
			switch (typeof detail) {
				case 'string':
					o.company = detail;
					o.tel = '';
					break;
				case 'object':
					if (Array.isArray(detail)) {
						o.company = detail[0];
						o.tel = detail[1];
					}
					break;
			}
		}
		if (!o.hasOwnProperty('company')) {
			o.company = '';
		}
		if (!o.hasOwnProperty('tel')) {
			o.tel = '';
		}
	}
	return list;
}